#!/bin/zsh

##########################################################
#
# title:     power completion (abbreviations) config file
#
# filename:  /etc/zsh/aliases/power_comp
#
# author(s): Alessandro Molari (mola89.molarulez.com),
#            Luca Molari
#
# license:   This file is licensed under the GPLv2
#            For more informations read:
#            http://www.gnu.org/licenses/gpl-2.0.txt
#
# thanks to: zsh team (www.zsh.org)
#            grml team (grml.org)
#            Michael Prokop (mika@grml.org)
#
##########################################################



declare -A abbreviations
abbreviations=(
    "ls"        "ls -h --color=auto"
    "ll"        "ls -lh --color=auto"
    "la"        "ls -alh --color=auto"
    "lr"        "ls -alhRr --color=auto"
    "ldot"      "ls -alh .*(.)"
    "ld"        "ls -dlh *(/)"
    "lde"       "ls -dl *(/^F)"
    "lns"       "ln -s"
    "grepc"     "grep --color=auto"
    "psa"       "ps -ely"
    "cpr"       "cp -r"
    "rr"        "rm -r"
    "md"        "mkdir -p"
    "duh"       "du -ch"
    "dus"       "du -chs"
    "dft"       "df -h --total"
    "pacmanc"   "pacman-color"
)

help-abbrevs() {
    local i=1

    echo "expansion character\t=>\t<space>"
    echo "prevent expansions\t=>\t<ctrl>+x"
    echo ""
    while [ $i -le ${#abbreviations} ]
    do
        echo ${${(k)abbreviations}[${i}]}"\t\t=>\t"${${abbreviations}[${i}]}
        ((i=i+1))
    done
}

magic-abbrev-expand() {
    local MATCH
    LBUFFER=${LBUFFER%%(#m)[_a-zA-Z0-9]#}
    LBUFFER+=${abbreviations[$MATCH]:-$MATCH}
    zle self-insert
}

no-magic-abbrev-expand() {
  LBUFFER+=' '
}

zle -N magic-abbrev-expand
zle -N no-magic-abbrev-expand
